<template>
  <div class="min-h-[calc(100vh-120px)]">
    <el-main>
      <div class="max-w-1200px mx-auto px-20px">
        <div class="bread-box flex items-center justify-start">
          <div class="line-height-20px">当前位置：</div>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>需方市场</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-card
          class="rounded-8px"
          shadow="never"
        >

          <!-- 产业分类 Tab -->
          <div class="mb-20px">
            <el-row
              :gutter="20"
              class="filter-row flex items-start"
            >
              <el-col :span="2">
                <h4>产业分类</h4>
              </el-col>
              <el-col :span="22">
                <el-tabs
                  v-model="productTypeId"
                  type="card"
                  @tab-click="productTypeChange"
                  class="custom-tabs flex-1"
                >
                  <el-tab-pane
                    label="不限"
                    name="-"
                  ></el-tab-pane>
                  <el-tab-pane
                    v-for="(value,index) in types"
                    :key="index"
                    :label="value.typeName"
                    :name="value.typeId"
                  ></el-tab-pane>
                </el-tabs>
              </el-col>
            </el-row>
          </div>

          <!-- 所属区域 Tab -->
          <div class="mb-20px">
            <el-row
              :gutter="20"
              class="filter-row flex items-start"
            >
              <el-col :span="2">
                <h4>所属区域</h4>
              </el-col>
              <el-col :span="22">
                <el-tabs
                  v-model="companyDivisionCode"
                  type="card"
                  @tab-click="companyDivisionChange"
                  class="custom-tabs flex-1"
                >
                  <el-tab-pane
                    label="不限"
                    name="-"
                  ></el-tab-pane>
                  <el-tab-pane
                    v-for="(value,index) in citys"
                    :key="index"
                    :label="value.prefecture"
                    :name="value.code"
                  ></el-tab-pane>
                </el-tabs>
              </el-col>
            </el-row>
          </div>
          <el-divider></el-divider>
          <!-- 搜索框 -->
          <div class="w-500px">
            <el-row
              :gutter="20"
              class="items-center flex"
            >
              <el-col :span="4">
                <h4 class="line-height-40px m-0">关键字</h4>
              </el-col>
              <el-col :span="16">
                <el-input
                  v-model="searchKeyword"
                  placeholder="请输入关键字"
                  clearable
                  @keyup.enter.native="getListData"
                ></el-input>
              </el-col>
              <el-col :span="4">
                <el-button
                  type="primary"
                  @click="getListData"
                  class="ml-10px"
                >
                  搜索
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-card>

        <div class="mt-20px">
          <div class="suppliers">
            <el-row :gutter="20">
              <el-col
                :span="8"
                v-for="supplier in suppliers"
                :key="supplier.id"
              >
                <el-card
                  class="mb-20px h-300px flex flex-col"
                  shadow="hover"
                  :body-style="{ padding: '0px' }"
                  @click.native="handleSupplierClick(supplier)"
                >
                  <div class="flex flex-col h-full p-20px 30px">
                    <p class="m-0 mb-5px font-bold text-16px">{{ supplier.requirement }}</p>
                    <div class="mt-20px">
                      <p class=" text-[#6C6C6C] mt-10px">
                        <span class=" text-[#8F8F8F]">需求类型：</span>{{ supplier.requirementTypeName }}
                      </p>
                      <p class=" text-[#6C6C6C] mt-10px">
                        <span class=" text-[#8F8F8F]">有效时间：</span>
                        <span class=" text-[#0669C8]">{{ supplier.limitTime }}</span>
                      </p>
                      <p class=" text-[#6C6C6C] mt-10px">
                        <span class=" text-[#8F8F8F]">需求企业：</span>{{ supplier.companyName }}
                      </p>
                    </div>
                    <div class=" bg-[#F6F7F9] flex items-center justify-around mt-20px p-20px text-center">
                      <div>
                        <h3 class=" text-[#0669C8]">{{ supplier.areaName }}</h3>
                        <div>所属地区</div>
                      </div>
                      <div class="w-1px h-50px bg-[#E5E5E5]"></div>
                      <div>
                        <h3 class=" text-[#0669C8]">{{ supplier.budget ? `${supplier.budget}元` : '需服务商报价'  }}</h3>
                        <div>最高预算</div>
                      </div>
                    </div>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </div>
        </div>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          @current-change="handlePageChange"
          class="flex justify-end"
        ></el-pagination>
      </div>
    </el-main>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getRequirementList } from "../api/home";
export default {
  name: "demandMarket",
  data() {
    return {
      productTypeId: "-",
      companyDivisionCode: "-",
      searchKeyword: "",
      suppliers: [],
      total: 100,
      currentPage: 1,
      pageSize: 6,
    };
  },
  computed: {
    ...mapGetters("dicts", ["cityDict", "getDictByParamsKey"]),
    citys() {
      return this.cityDict;
    },
    types() {
      return this.getDictByParamsKey({ parentId: 2 });
    },
    purchases() {
      return this.getDictByParamsKey({ parentId: 4 });
    },
  },
  async mounted() {
    this.getListData();
  },
  methods: {
    getListData() {
      const params = {
        isAsc: true,
        pageNo: this.currentPage,
        pageNum: this.pageSize,
        q: this.searchKeyword,
      };
      if (this.productTypeId != "-") {
        params.productTypeId = this.productTypeId;
      }
      if (this.companyDivisionCode != "-") {
        params.companyDivisionCode = this.companyDivisionCode;
      }
      getRequirementList(params).then((res) => {
        this.suppliers = res.rows;
        this.total = res.total;
      });
    },
    productTypeChange(e) {
      this.productTypeId = e.name;
      this.getListData();
    },
    companyDivisionChange(e) {
      this.companyDivisionCode = e.name;
      this.getListData();
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.getListData();
    },
    handleSupplierClick(supplier) {
      this.$router.push({
        name: "demandMarketDetail",
        query: { id: supplier.requirementId },
      });
    },
  },
};
</script>

<style scoped>
/* 自定义 Tabs 样式 */
.custom-tabs ::v-deep .el-tabs__header {
  margin-bottom: 0 !important;
  border-bottom: none !important;
}

.custom-tabs ::v-deep .el-tabs__nav {
  border: none !important;
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
}

.custom-tabs ::v-deep .el-tabs__item {
  height: 30px !important;
  line-height: 30px !important;
  padding: 0 12px !important;
  border: none !important;
  background: transparent !important;
  border-radius: 0 !important;
  flex-shrink: 0; /* 防止压缩 */
}

.custom-tabs ::v-deep .el-tabs__item.is-active {
  background-color: #0094de !important;
  color: white !important;
  border: none !important;
}

.custom-tabs ::v-deep .el-tabs__item:not(.is-active):hover {
  color: #409eff !important;
}

.bread-box {
  display: flex !important;
  align-items: center !important;
  justify-content: start !important;
}

.el-breadcrumb {
  padding: 15px !important;
  border-radius: 4px !important;
}

::v-deep .el-pagination.is-background .btn-next,
::v-deep .el-pagination.is-background .btn-prev,
::v-deep .el-pagination.is-background .el-pager li {
  background-color: #fff !important;
}

::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
  color: #596df4 !important;
  border: 1px solid #596df4 !important;
}
</style>